package edu.ccut.saturn.studio.wfg.dialog.formdatadialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.wfg.dialog.KeyLabelPrivoder;
import edu.ccut.saturn.studio.wfg.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.wfg.model.Shape;
import edu.ccut.saturn.studio.wfg.wfgmodel.FormDataAttribute;

public class SaturnFormDataDialogForEdit extends TitleAreaDialog {
	
	private Shape shape;

	private Label IDLabel, expressionLabel, browser, nameLabel, typeLabel, accessTypeLabel, requiredLabel;

	private Text IDText, expressionText, nameText;
	
	private Combo typeCombo, accessTypeCombo, requiredCombo;
	
	private FormDataAttribute formDataAttribute;
	
	public SaturnFormDataDialogForEdit(Shell parent, FormDataAttribute formDataAttribute, Shape shape) {
		super(parent);
		this.shape = shape;
		this.formDataAttribute = formDataAttribute;
	}

	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(Messages.getString("SaturnFormDataDialogForEdit.ConfigEditeMenu"));   //$NON-NLS-1$
		return contents;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		
		getShell().setText(SelectionCandidate.ATTRIBUTE_SETTING);
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		
		Composite area = (Composite) super.createDialogArea(parent);
		final Composite composite = new Composite(area, SWT.NONE);
		
		final GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true);
		composite.setLayoutData(gd_composite);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		composite.setLayout(gridLayout);

		IDLabel = new Label(composite, SWT.NONE);
		final GridData gd_iDLabel = new GridData();
		gd_iDLabel.verticalIndent = 5;
		gd_iDLabel.horizontalIndent = 2;
		IDLabel.setLayoutData(gd_iDLabel);
		IDLabel.setText("ID:"); 

		IDText = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_iDText = new GridData(SWT.FILL, SWT.CENTER, true, false,2, 1);
		gd_iDText.verticalIndent = 5;
		IDText.setLayoutData(gd_iDText);

		expressionLabel = new Label(composite, SWT.NONE);
		final GridData gd_expressionLabel = new GridData();
		gd_expressionLabel.horizontalIndent = 2;
		gd_expressionLabel.verticalIndent = 5;
		expressionLabel.setLayoutData(gd_expressionLabel);
		expressionLabel.setText(Messages.getString("SaturnFormDataDialogForEdit.Expression"));   //$NON-NLS-1$

		expressionText = new Text(composite, SWT.BORDER);
		final GridData gd_expressionText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_expressionText.verticalIndent = 5;
		expressionText.setLayoutData(gd_expressionText);
		
		browser = new Label(composite, SWT.NONE);
		final GridData gd_browser = new GridData();
		gd_browser.verticalIndent = 5;
		browser.setLayoutData(gd_browser);
		browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS)); 
		browser.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				browser.setImage(Activator.getImage(Images.AMPLIFY_MINUS)); 
			}
			public void mouseExit(final MouseEvent e) {
				browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS)); 
			}
		});

		nameLabel = new Label(composite, SWT.NONE);
		final GridData gd_nameLabel = new GridData();
		gd_nameLabel.horizontalIndent = 2;
		gd_nameLabel.verticalIndent = 5;
		nameLabel.setLayoutData(gd_nameLabel);
		nameLabel.setText(Messages.getString("SaturnFormDataDialogForEdit.Name"));   //$NON-NLS-1$

		nameText = new Text(composite, SWT.BORDER);
		final GridData gd_nameText = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_nameText.verticalIndent = 5;
		nameText.setLayoutData(gd_nameText);

		typeLabel = new Label(composite, SWT.NONE);
		final GridData gd_typeLabel = new GridData();
		gd_typeLabel.horizontalIndent = 2;
		gd_typeLabel.verticalIndent = 5;
		typeLabel.setLayoutData(gd_typeLabel);
		typeLabel.setText(Messages.getString("SaturnFormDataDialogForEdit.Type"));   //$NON-NLS-1$

		typeCombo = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_typeCombo = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_typeCombo.verticalIndent = 5;
		typeCombo.setLayoutData(gd_typeCombo);
		typeCombo.setItems(SelectionCandidate.formDataTypes);

		accessTypeLabel = new Label(composite, SWT.NONE);
		final GridData gd_accessTypeLabel = new GridData();
		gd_accessTypeLabel.horizontalIndent = 2;
		gd_accessTypeLabel.verticalIndent = 5;
		accessTypeLabel.setLayoutData(gd_accessTypeLabel);
		accessTypeLabel.setText(Messages.getString("SaturnFormDataDialogForEdit.ReadType"));   //$NON-NLS-1$

		accessTypeCombo = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_accessTypeCombo = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_accessTypeCombo.verticalIndent = 5;
		accessTypeCombo.setLayoutData(gd_accessTypeCombo);
		accessTypeCombo.setItems(SelectionCandidate.formDataReadWrite);

		requiredLabel = new Label(composite, SWT.NONE);
		final GridData gd_requiredLabel_1 = new GridData();
		gd_requiredLabel_1.horizontalIndent = 2;
		gd_requiredLabel_1.verticalIndent = 5;
		requiredLabel.setLayoutData(gd_requiredLabel_1);
		requiredLabel.setText(Messages.getString("SaturnFormDataDialogForEdit.WhetherMust"));   //$NON-NLS-1$

		requiredCombo = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_requiredCombo = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_requiredCombo.horizontalIndent = 2;
		gd_requiredCombo.verticalIndent = 5;
		requiredCombo.setLayoutData(gd_requiredCombo);
		requiredCombo.setItems(SelectionCandidate.formDataRequired);
		
		IDText.setText(formDataAttribute.getId().trim());
		expressionText.setText(formDataAttribute.getKey().trim());
		nameText.setText(formDataAttribute.getName().trim());
		typeCombo.setText(formDataAttribute.getType().trim());
		accessTypeCombo.setText(formDataAttribute.getAccessType().trim());
		requiredCombo.setText(formDataAttribute.getRequired().trim());
		
		browser.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				ElementListSelectionDialog dialog1 = new ElementListSelectionDialog(
						Display.getDefault().getShells()[0], new KeyLabelPrivoder());
				dialog1.setTitle(Messages.getString("SaturnFormDataDialogForEdit.ChooseOneExpression"));  //$NON-NLS-1$
				dialog1.setElements(shape.getFormDataList());
				
				if (dialog1.open() == Window.OK) {
					String ddd = (String) dialog1.getFirstResult();
					expressionText.setText(ddd);
					
					nameText.setText(shape.getFormDataInformationByKey(ddd)[0]);
					typeCombo.setText(shape.getFormDataInformationByKey(ddd)[1]);
					accessTypeCombo.setText(shape.getFormDataInformationByKey(ddd)[2]);
					requiredCombo.setText(shape.getFormDataInformationByKey(ddd)[3]);
				}
			}
		});
		
		return area;
	}

	@Override
	protected Point getInitialSize() {
		return new Point(400, 330);
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if(buttonId == IDialogConstants.OK_ID){
			formDataAttribute.setId(IDText.getText().trim());
			formDataAttribute.setKey(expressionText.getText().trim());
			formDataAttribute.setName(nameText.getText().trim());
			formDataAttribute.setType(typeCombo.getText().trim());
			formDataAttribute.setAccessType(accessTypeCombo.getText().trim());
			formDataAttribute.setRequired(requiredCombo.getText().trim());
		}
		super.buttonPressed(buttonId);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
	}
}
